package com.cherrish;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author cherrish
 * @time 2018-08-30 16:16
 * @name SinkSender
 * @desc:
 */
@EnableBinding(value = {Source.class})
public class SinkSender {
    private static Logger log = LoggerFactory.getLogger(SinkSender.class);
    private final static AtomicInteger NUM = new AtomicInteger(0);
    @InboundChannelAdapter(value = Source.OUTPUT,poller = @Poller(fixedRate = "3000"))
    public String timerMessageSource() {
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.info(NUM.getAndIncrement() + " Send message : " + format);
        return format;
    }

    @Autowired
    Source source;
    public void send(String message){
        source.output().send(org.springframework.integration.support.MessageBuilder.withPayload(message).build());
    }
}

